package com.dudu.access;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.dudu.bean.ResponseSendVoiceBean;
import com.dudu.service.ILogService;
import com.dudu.service.impl.LogService;
import com.dudu.utils.DateUtils;
import com.dudu.utils.Encrypt;
import com.dudu.utils.HttpRequest;
import com.dudu.utils.ProperyResource;

/**
 * IMS语音通知 /IMS语音验证码
 * 
 * @author gaoxs
 * 
 */
public class ImsVoiceNoticeUtils {

    private static final Logger log = core.util.Logger.getLogger(ImsVoiceNoticeUtils.class.getName());

    private static ILogService logService = new LogService();

    /**
     * 推送地址设置
     * 
     * @param url
     * @return
     */
    public static ResponseSendVoiceBean setCallStatusUrl(String url) {

	ResponseSendVoiceBean responseSendVoice = new ResponseSendVoiceBean();

	StringBuffer urlBuffer = new StringBuffer(ProperyResource.getServiceUrl());
	urlBuffer.append("?postData=<Request><Head><MethodName>CallStatusUrl</MethodName>");
	urlBuffer.append("<Spid>" + ProperyResource.getSpId() + "</Spid>");
	urlBuffer.append("<Appid>" + ProperyResource.getAppId() + "</Appid>");
	urlBuffer.append("<Passwd>" + Encrypt.sha1Encrypt(ProperyResource.getPassword()) + "</Passwd>");
	urlBuffer.append("</Head><Body><Url>" + url + "</Url><Body></Request>");
	String returnData = HttpRequest.getData(urlBuffer.toString());
	String result = "";

	try {

	    Document doc = DocumentHelper.parseText(returnData);
	    Element root = doc.getRootElement();
	    Element head = root.element("Head");
	    result = head.elementTextTrim("Result");

	} catch (DocumentException e) {
	    e.printStackTrace();
	}

	if (StringUtils.isNotBlank(result) && !result.equals("0")) {
	    log.info("发送语音通知失败");
	}

	responseSendVoice.setResult(result);

	return responseSendVoice;

    }

    /**
     * 用户按键回调地址设置接口
     * 
     * @param voiceTransferUrl
     * @return
     */
    public static JSONObject VoiceTransferUrl(String voiceTransferUrl) {
	log.info("ImsUtils VoiceTransferUrl");
	JSONObject object = new JSONObject();
	long begin = 0;
	long end = 0;
	double during;
	// 返回值
	String result_desc = "";
	// 返回状态
	String result_status = "";
	// 发起时间
	String currentTime = DateUtils.getCurrentTime();

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
	String timestamp = sdf.format(Calendar.getInstance().getTime());
	// 请求参数
	StringBuffer urlBuffer = new StringBuffer(ProperyResource.getServiceUrl());
	urlBuffer.append("?postData=<Request><Head><MethodName>VoiceTransferUrl</MethodName>");
	urlBuffer.append("<Spid>").append(ProperyResource.getSpId()).append("</Spid>");
	urlBuffer.append("<Appid>").append(ProperyResource.getAppId()).append("</Appid>");
	urlBuffer.append("<Passwd>").append(Encrypt.sha1Encrypt(ProperyResource.getPassword())).append("</Passwd>");
	urlBuffer.append("<Timestamp>").append(timestamp).append("</Timestamp>");
	urlBuffer.append("<Authenticator>").append(Encrypt.sha1Encrypt(timestamp + "IMS_QUERY" + ProperyResource.getSpId() + ProperyResource.getPassword()))
		.append("</Authenticator>");
	urlBuffer.append("</Head><Body>");
	// 地址进行base64加密
	urlBuffer.append("<Url>").append(Encrypt.base64Encoder(voiceTransferUrl)).append("</Url>");

	urlBuffer.append("</Body></Request>");

	log.info("VoiceNotice5 url:" + urlBuffer);
	try {
	    // 调用开始时间点
	    begin = Calendar.getInstance().getTimeInMillis();
	    // 获取返回值
	    String data = HttpRequest.getDataThreeTime(urlBuffer.toString());
	    // 调用结束时间点
	    end = Calendar.getInstance().getTimeInMillis();

	    log.info("VoiceTransferUrl response:" + data);
	    // 解析返回值
	    Document doc = DocumentHelper.parseText(data);
	    Element root = doc.getRootElement();
	    Element head = root.element("Head");
	    // 写入最终结果
	    object.put("result", head.elementTextTrim("Result"));
	    // 写入描述
	    object.put("discripe", head.elementTextTrim("ResultDesc"));
	    // 写入编码
	    object.put("code", "0000");

	    result_desc = data;
	    result_status = head.elementTextTrim("Result");
	} catch (Exception e) {
	    log.info("IMS用户按键回调地址设置接口出现异常！");
	    result_desc = "VoiceTransferUrl 接口调用异常";
	    result_status = "-1";
	}

	try {
	    during = Double.valueOf((end - begin)) / 1000;
	    logService.logIms(during, "VoiceTransferUrl", urlBuffer.toString(), result_desc, result_status, currentTime, "IMS", "");
	} catch (Exception e) {
	    log.info("IMS用户按键回调地址设置接口出现异常！");
	}

	return object;
    }

    /**
     * TTS语音通知-----TTS语音转换
     * 
     * @param ttsContent
     * @return
     * @throws UnsupportedEncodingException
     */
    public static ResponseSendVoiceBean createTTSVoice(String ttsContent) throws UnsupportedEncodingException {
	// 开始时间
	long begin = 0;
	// 结束时间
	long end = 0;
	String currentTime = DateUtils.getCurrentTime();

	ResponseSendVoiceBean responseSendVoice = new ResponseSendVoiceBean();
	// 记录发起的开始时间
	begin = Calendar.getInstance().getTimeInMillis();
	StringBuffer urlBuffer = new StringBuffer(ProperyResource.getServiceUrl());
	urlBuffer.append("?postData=<Request><Head><MethodName>CreateTTSVoice</MethodName>");
	urlBuffer.append("<Spid>" + ProperyResource.getSpId() + "</Spid>");
	urlBuffer.append("<Appid>" + ProperyResource.getAppId() + "</Appid>");
	urlBuffer.append("<Passwd>" + Encrypt.sha1Encrypt(ProperyResource.getPassword()) + "</Passwd>");
	urlBuffer.append("</Head><Body><TTSContent>" + URLEncoder.encode(ttsContent, "utf-8") + "</TTSContent></Body></Request>");

	String returnData = HttpRequest.getPostToVoice(urlBuffer.toString());
	// 记录结束时间
	end = Calendar.getInstance().getTimeInMillis();
	String result = "";
	String fileName = "";

	try {

	    Document doc = DocumentHelper.parseText(returnData);
	    Element root = doc.getRootElement();
	    Element head = root.element("Head");
	    result = head.elementTextTrim("Result");
	    fileName = head.elementTextTrim("FileName");

	} catch (DocumentException e) {
	    e.printStackTrace();
	}

	if (StringUtils.isNotBlank(result) && !result.equals("0")) {
	    log.info("发送语音通知失败");
	}

	responseSendVoice.setResult(result);
	responseSendVoice.setFileName(fileName);

	try {
	    Double during = Double.valueOf((end - begin)) / 1000;
	    String request_msg = urlBuffer.toString();
	    logService.logIms(during, "createTTSVoice", request_msg.length() > 1998 ? request_msg.substring(0, 1000) : request_msg, returnData, result, currentTime, "IMS", "");
	} catch (Exception e) {
	    log.error("------IMS记录语音短信日志异常！----" + e);
	}

	return responseSendVoice;

    }

    /**
     * TTS语音通知--TTS语音通知 发起语音留言
     * 
     * @param chargeNbr
     *            IMS提供用户名
     * @param chargeKey
     *            IMS提供密码
     * @param displayNbr
     *            显示号码
     * @param CalleeNbr
     *            被叫号码
     * @param VoiceName
     *            发起语音文件名称
     * @param tts_content
     *            前导音
     * @return
     */
    public static ResponseSendVoiceBean sendVoiceNotice3(String chargeNbr, String chargeKey, String displayNbr, String CalleeNbr, String VoiceName, String tts_content) {

	log.info("sendVoiceNotice3 begin = " + DateUtils.getCurrentTime());
	// 开始时间
	long begin = 0;
	// 结束时间
	long end = 0;
	String currentTime = DateUtils.getCurrentTime();

	ResponseSendVoiceBean responseSendVoice = new ResponseSendVoiceBean();
	StringBuffer urlBuffer = new StringBuffer(ProperyResource.getServiceUrl());
	urlBuffer.append("?postData=<Request><Head><MethodName>VoiceNotice3</MethodName>");
	urlBuffer.append("<Spid>" + ProperyResource.getSpId() + "</Spid>");
	urlBuffer.append("<Appid>" + ProperyResource.getAppId() + "</Appid>");
	urlBuffer.append("<Passwd>" + Encrypt.sha1Encrypt(ProperyResource.getPassword()) + "</Passwd>");
	urlBuffer.append("</Head><Body><ChargeNbr>" + chargeNbr.trim() + "</ChargeNbr><Key>" + chargeKey.trim() + "</Key>");

	urlBuffer.append("<DisplayNbr>" + displayNbr.trim() + "</DisplayNbr>");
	urlBuffer.append("<CalleeNbr>" + CalleeNbr + "</CalleeNbr>");
	urlBuffer.append("<VoiceName>" + VoiceName + "</VoiceName>");
	if (StringUtils.isNotBlank(tts_content)) {
	    try {
		// urlBuffer.append("<TTSContent>"+tts_content+"</TTSContent>");
		urlBuffer.append("<TTSContent>" + URLEncoder.encode(tts_content, "utf-8") + "</TTSContent>");
		// } catch (UnsupportedEncodingException e) {
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	}
	urlBuffer.append("<vReplay>1</vReplay></Body></Request>");
	// 记录发起的开始时间
	begin = Calendar.getInstance().getTimeInMillis();
	// ---20160129修改，变更提交方式

	String returnData = HttpRequest.getPostToVoice(urlBuffer.toString());
	// 记录结束时间
	end = Calendar.getInstance().getTimeInMillis();
	String result = "";
	String sessionid = "";
	String desc = "";
	if (StringUtils.isNotBlank(returnData)) {
	    try {

		Document doc = DocumentHelper.parseText(returnData);
		Element root = doc.getRootElement();
		Element head = root.element("Head");
		Element body = root.element("Body");
		result = head.elementTextTrim("Result");
		sessionid = head.elementTextTrim("Sessionid");
		desc = head.elementTextTrim("ResultDesc");
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	} else {
	    returnData = "";
	    result = "-1";
	}

	if (StringUtils.isNotBlank(result) && !result.equals("0")) {
	    log.info("发送语音通知失败");
	}

	responseSendVoice.setResult(result);
	responseSendVoice.setSessionid(sessionid);
	responseSendVoice.setDesc(desc);

	try {
	    Double during = Double.valueOf((end - begin)) / 1000;
	    logService.logIms(during, "sendVoiceNotice3", urlBuffer.toString(), returnData, result, currentTime, "IMS", CalleeNbr);
	} catch (Exception e) {
	    log.info("------IMS记录语音短信日志异常！----");
	}

	log.info("sendVoiceNotice3 end = " + DateUtils.getCurrentTime());

	return responseSendVoice;

    }

    /**
     * TTS语音通知--TTS语音通知 发起语音留言
     * 
     * @param chargeNbr
     *            IMS提供用户名
     * @param chargeKey
     *            IMS提供密码
     * @param displayNbr
     *            显示号码
     * @param CalleeNbr
     *            被叫号码
     * @param tts_content
     *            文字
     * @return
     */
    public static ResponseSendVoiceBean sendNoticeForthwith(String chargeNbr, String chargeKey, String displayNbr, String CalleeNbr, String tts_content) {

	log.info("sendNoticeForthwith begin = " + DateUtils.getCurrentTime());
	// 开始时间
	long begin = 0;
	// 结束时间
	long end = 0;
	String currentTime = DateUtils.getCurrentTime();

	ResponseSendVoiceBean responseSendVoice = new ResponseSendVoiceBean();
	StringBuffer urlBuffer = new StringBuffer(ProperyResource.getServiceUrl());
	urlBuffer.append("?postData=<Request><Head><MethodName>VoiceNotice3</MethodName>");
	urlBuffer.append("<Spid>" + ProperyResource.getSpId() + "</Spid>");
	urlBuffer.append("<Appid>" + ProperyResource.getAppId() + "</Appid>");
	urlBuffer.append("<Passwd>" + Encrypt.sha1Encrypt(ProperyResource.getPassword()) + "</Passwd>");
	urlBuffer.append("</Head><Body><ChargeNbr>" + chargeNbr.trim() + "</ChargeNbr><Key>" + chargeKey.trim() + "</Key>");

	urlBuffer.append("<DisplayNbr>" + displayNbr.trim() + "</DisplayNbr>");
	urlBuffer.append("<CalleeNbr>" + CalleeNbr + "</CalleeNbr>");
	if (StringUtils.isNotBlank(tts_content)) {
	    try {
		urlBuffer.append("<TTSContent>" + URLEncoder.encode(tts_content, "utf-8") + "</TTSContent>");
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	}
	urlBuffer.append("<ReplayTTS>1</ReplayTTS>");
	urlBuffer.append("<vReplay>1</vReplay></Body></Request>");
	// 记录发起的开始时间
	begin = Calendar.getInstance().getTimeInMillis();
	// 调用地址
	String returnData = HttpRequest.getPostToVoice(urlBuffer.toString());
	// 记录结束时间
	end = Calendar.getInstance().getTimeInMillis();
	String result = "";
	String sessionid = "";
	try {

	    Document doc = DocumentHelper.parseText(returnData);
	    Element root = doc.getRootElement();
	    Element head = root.element("Head");
	    Element body = root.element("Body");
	    result = head.elementTextTrim("Result");
	    sessionid = head.elementTextTrim("Sessionid");

	} catch (DocumentException e) {
	    e.printStackTrace();
	}

	if (StringUtils.isNotBlank(result) && !result.equals("0")) {
	    log.info("发送语音通知失败");
	}

	responseSendVoice.setResult(result);
	responseSendVoice.setSessionid(sessionid);

	try {
	    Double during = Double.valueOf((end - begin)) / 1000;
	    logService.logIms(during, "sendNoticeForthwith", urlBuffer.toString(), returnData, result, currentTime, "IMS", "");
	} catch (Exception e) {
	    log.info("------IMS记录语音短信日志异常！----");
	}

	log.info("sendNoticeForthwith end = " + DateUtils.getCurrentTime());

	return responseSendVoice;

    }

    /**
     * 语音验证码发送--VoiceCaptcha 发起语音验证码
     * 
     * @param chargeNbr
     *            IMS提供用户名
     * @param chargeKey
     *            IMS提供密码
     * @param displayNbr
     *            显示号码
     * @param CalleeNbr
     *            被叫号码
     * @param tts_content
     *            文字
     * @return
     */
    public static ResponseSendVoiceBean sendIMSVoiceCaptcha(String chargeNbr, String chargeKey, String displayNbr, String CalleeNbr, String tts_content) {

	log.info("sendVoiceCaptcha begin = " + DateUtils.getCurrentTime());
	// 开始时间
	long begin = 0;
	// 结束时间
	long end = 0;
	String currentTime = DateUtils.getCurrentTime();

	ResponseSendVoiceBean responseSendVoice = new ResponseSendVoiceBean();
	StringBuffer urlBuffer = new StringBuffer();
	urlBuffer.append("<Request><Head><MethodName>VoiceCaptcha</MethodName>");
	urlBuffer.append("<Spid>" + ProperyResource.getSpId() + "</Spid>");
	urlBuffer.append("<Appid>" + ProperyResource.getAppId() + "</Appid>");
	urlBuffer.append("<Passwd>" + Encrypt.sha1Encrypt(ProperyResource.getPassword()) + "</Passwd>");
	urlBuffer.append("</Head><Body><ChargeNbr>" + chargeNbr.trim() + "</ChargeNbr><Key>" + chargeKey.trim() + "</Key>");

	urlBuffer.append("<DisplayNbr>" + displayNbr.trim() + "</DisplayNbr>");
	urlBuffer.append("<CalleeNbr>" + CalleeNbr + "</CalleeNbr>");
	// 您本次的注册验证码【Value1】，在【Value2】分钟内输入有效。【Value3】
	urlBuffer.append("<TempletId>10000</TempletId>");
	// 输入字符“0”或“1”来判断是否重听。0或为空，则默认不重听。1则提示“重听请按任意键，结束请按挂机键。”
	urlBuffer.append("<vReplay>1</vReplay>");
	urlBuffer.append("<ReplayTTS>1</ReplayTTS>");
	urlBuffer.append("<ReplayTimes>3</ReplayTimes>");
	if (StringUtils.isNotBlank(tts_content)) {
	    try {
		urlBuffer.append("<Value1>" + URLEncoder.encode(tts_content, "utf-8") + "</Value1>");
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	}
	urlBuffer.append("<Value2>10</Value2>");
	urlBuffer.append("</Body></Request>");
	String url=ProperyResource.getServiceUrl()+"?postData="+URLEncoder.encode(urlBuffer.toString());
	log.info("-------sendIMSVoiceCaptcha url:" + url);
	// 记录发起的开始时间
	begin = Calendar.getInstance().getTimeInMillis();
	// 调用地址
	String returnData = HttpRequest.getPostToVoice(url);
	// 记录结束时间
	end = Calendar.getInstance().getTimeInMillis();
	String result = "";
	String sessionid = "";
	log.info("returnData==" + returnData);
	log.info("urlBuffer==" + urlBuffer);
	try {

	    Document doc = DocumentHelper.parseText(returnData);
	    Element root = doc.getRootElement();
	    Element head = root.element("Head");
	    Element body = root.element("Body");
	    result = head.elementTextTrim("Result");
	    sessionid = head.elementTextTrim("Sessionid");

	} catch (DocumentException e) {
	    e.printStackTrace();
	}

	if (StringUtils.isNotBlank(result) && !result.equals("0")) {
	    log.info("发起语音验证码失败");
	}

	responseSendVoice.setResult(result);
	responseSendVoice.setSessionid(sessionid);

	try {
	    Double during = Double.valueOf((end - begin)) / 1000;
	    logService.logIms(during, "sendVoiceCaptcha", url, returnData, result, currentTime, "IMS", "");
	} catch (Exception e) {
	    log.info("------IMS记录发起语音验证码日志异常！----");
	}

	log.info("sendVoiceCaptcha end = " + DateUtils.getCurrentTime());

	return responseSendVoice;

    }

    /**
     * 普通语音转坐席非模板形式接口
     * 
     * @param displayNbr
     * @param chargeNbr
     * @param key
     * @param calleeNbr
     * @param templetId
     * @param TTSContent1
     * @param TTSContent2
     * @param TTSContent3
     * @param TTSContent4
     * @param TTSContent5
     * @param TTSContent6
     * @return
     */
    public static JSONObject VoiceNotice5(String displayNbr, String chargeNbr, String key, String calleeNbr, String templetId, String TTSContent1, String TTSContent2,
	    String TTSContent3, String TTSContent4, String TTSContent5, String TTSContent6) {
	log.debug("ImsUtils VoiceNotice5");
	JSONObject object = new JSONObject();
	long begin = 0;
	long end = 0;
	double during;
	// 返回值
	String result_desc = "";
	// 返回状态
	String result_status = "";
	// 发起时间
	String currentTime = DateUtils.getCurrentTime();

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
	String timestamp = sdf.format(Calendar.getInstance().getTime());
	// 请求参数
	StringBuffer urlBuffer = new StringBuffer(ProperyResource.getServiceUrl());
	urlBuffer.append("?postData=<Request><Head><MethodName>VoiceNotice5</MethodName>");
	urlBuffer.append("<Spid>").append(ProperyResource.getSpId()).append("</Spid>");
	urlBuffer.append("<Appid>").append(ProperyResource.getAppId()).append("</Appid>");
	urlBuffer.append("<Passwd>").append(Encrypt.sha1Encrypt(ProperyResource.getPassword())).append("</Passwd>");
	urlBuffer.append("<Timestamp>").append(timestamp).append("</Timestamp>");
	urlBuffer.append("<Authenticator>").append(Encrypt.sha1Encrypt(timestamp + "IMS_QUERY" + ProperyResource.getSpId() + ProperyResource.getPassword()))
		.append("</Authenticator>");
	urlBuffer.append("</Head><Body>");

	urlBuffer.append("<DisplayNbr>").append(displayNbr.trim()).append("</DisplayNbr>");
	urlBuffer.append("<ChargeNbr>").append(chargeNbr.trim()).append("</ChargeNbr>");
	urlBuffer.append("<Key>").append(key.trim()).append("</Key>");

	urlBuffer.append("<CalleeNbr>").append(calleeNbr).append("</CalleeNbr>");

	urlBuffer.append("<TempletId>").append(templetId).append("</TempletId>");
	urlBuffer.append("<TTSContent1>").append(TTSContent1).append("</TTSContent1>");
	urlBuffer.append("<TTSContent2>").append(TTSContent2).append("</TTSContent2>");
	urlBuffer.append("<TTSContent3>").append(TTSContent3).append("</TTSContent3>");
	urlBuffer.append("<TTSContent4>").append(TTSContent4).append("</TTSContent4>");
	urlBuffer.append("<TTSContent5>").append(TTSContent5).append("</TTSContent5>");
	urlBuffer.append("<TTSContent6>").append(TTSContent6).append("</TTSContent6>");

	urlBuffer.append("</Body></Request>");

	log.info("VoiceNotice5 url:" + urlBuffer);
	try {
	    // 调用开始时间点
	    begin = Calendar.getInstance().getTimeInMillis();
	    // 获取返回值
	    String data = HttpRequest.getDataThreeTime(urlBuffer.toString());
	    // 调用结束时间点
	    end = Calendar.getInstance().getTimeInMillis();

	    log.info("VoiceNotice5 response:" + data);
	    // 解析返回值
	    Document doc = DocumentHelper.parseText(data);
	    Element root = doc.getRootElement();
	    Element head = root.element("Head");
	    // 写入最终结果
	    object.put("result", head.elementTextTrim("Result"));
	    // 写入描述
	    object.put("discripe", head.elementTextTrim("ResultDesc"));
	    // 写入编码
	    object.put("code", "0000");

	    result_desc = data;
	    result_status = head.elementTextTrim("Result");
	} catch (Exception e) {
	    log.info("IMS普通语音转坐席非模板形式接口出现异常！");
	    result_desc = "VoiceNotice5 接口调用异常";
	    result_status = "-1";
	}

	try {
	    during = Double.valueOf((end - begin)) / 1000;
	    logService.logIms(during, "VoiceNotice5", urlBuffer.toString(), result_desc, result_status, currentTime, "IMS", "");
	} catch (Exception e) {
	    log.info("IMS普通语音转坐席非模板形式接口出现异常！");
	}

	return object;
    }

    /**
     * 问卷统计接口
     * 
     * @param displayNbr
     *            显号
     * @param chargeNbr
     *            计费账号
     * @param key
     *            账号KEY
     * @param calleeNbr
     *            被叫
     * @param VoiceName
     *            文件名称
     * @param ttsContent
     *            内容
     * @return
     */
    public static ResponseSendVoiceBean VoiceNotice2(String displayNbr, String chargeNbr, String key, String calleeNbr, String VoiceName, String ttsContent) {
	log.debug("ImsUtils VoiceNotice2");
	String currentTime = DateUtils.getCurrentTime();

	ResponseSendVoiceBean responseSendVoice = new ResponseSendVoiceBean();

	StringBuffer urlBuffer = new StringBuffer(ProperyResource.getServiceUrl());
	urlBuffer.append("?postData=<Request><Head><MethodName>VoiceNotice2</MethodName>");
	urlBuffer.append("<Spid>" + ProperyResource.getSpId() + "</Spid>");
	urlBuffer.append("<Appid>" + ProperyResource.getAppId() + "</Appid>");
	urlBuffer.append("<Passwd>" + Encrypt.sha1Encrypt(ProperyResource.getPassword()) + "</Passwd>");
	urlBuffer.append("</Head><Body><ChargeNbr>" + chargeNbr.trim() + "</ChargeNbr><Key>" + key.trim() + "</Key>");
	urlBuffer.append("<DisplayNbr>" + displayNbr.trim() + "</DisplayNbr>");
	urlBuffer.append("<CalleeNbr>" + calleeNbr.trim() + "</CalleeNbr>");
	urlBuffer.append("<VoiceName>" + VoiceName + "</VoiceName>");
	if (StringUtils.isNotBlank(ttsContent)) {
	    try {
		// urlBuffer.append("<TTSContent>"+tts_content+"</TTSContent>");
		urlBuffer.append("<TTSContent>" + URLEncoder.encode(ttsContent, "utf-8") + "</TTSContent>");
		// } catch (UnsupportedEncodingException e) {
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	}
	urlBuffer.append("</Body></Request>");
	// 记录发起的开始时间
	long begin = Calendar.getInstance().getTimeInMillis();
	String returnData = HttpRequest.getPostToVoice(urlBuffer.toString());
	long end = Calendar.getInstance().getTimeInMillis();
	String result = "";
	String sessionid = "";
	String desc = "";
	if (StringUtils.isNotBlank(returnData)) {
	    try {
		Document doc = DocumentHelper.parseText(returnData);
		Element root = doc.getRootElement();
		Element head = root.element("Head");
		result = head.elementTextTrim("Result");
		sessionid = head.elementTextTrim("Sessionid");
		desc = head.elementTextTrim("ResultDesc");
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	} else {
	    returnData = "";
	    result = "-1";
	}
	responseSendVoice.setResult(result);
	responseSendVoice.setSessionid(sessionid);
	responseSendVoice.setDesc(desc);
	try {
	    Double during = Double.valueOf((end - begin)) / 1000;
	    logService.logIms(during, "VoiceNotice2", urlBuffer.toString(), returnData, result, currentTime, "IMS", calleeNbr);
	} catch (Exception e) {
	    log.info("------IMS记录语音短信日志异常！----");
	}
	return responseSendVoice;
    }

    public static void main(String[] agr) {
	// VoiceNotice5("17001090102", "17001090102", "43147045", "17001090136",
	// "10001", "123456", "1", "危东", "", "", "");
	VoiceNotice2("8659527143066", "8659527143066", "50718314", "17001090120", "", "满意请按1,不满意请按2");
	sendIMSVoiceCaptcha("8659527143061","28039283","8659527143061","17001090120","4563");
    }
}
